<?php

/**
 * Контроллер административной панели, реализующий управление справочниками
 * 
 * @category Phorm
 * @package Controller_Module_Control
 * @name /Phorm/Controller/Module/Control/Dictionaries.php
 * @copyright phorm.ru
 * @since 05.05.2012
 * @author vladimir@kachalov.net
 */

class Phorm_Controller_Module_Control_Dictionaries extends Phorm_Controller_Controller {
	
	/**
	 * Экземпляр класса справочников
	 * @var Phorm_Dictionaries
	 */
	public $Dictionaries;
	
	
	/**
	 * Предиспетчер
	 * 
	 * @return void
	 */
		
	public function preDispatch() {
		
		$this->Dictionaries = new Phorm_Dictionaries();
		
	}
	
	/**
	 * Список справочников
	 * 
	 * @return void
	 */
		
	public function indexAction() {
		
		$options = Phorm_Config::xml('grids/control/dictionaries.xml','index');
		
		if($this->_request->isXmlHttpRequest()) {
			
			$rows = $this->Dictionaries->getDictionariesList($this->_params);
			$paginator = $this->Dictionaries->getPaginator();

			$this->_helper->json(Phorm_JQuery_DataGrid::data($rows,$paginator,$options));
			
		} else {
			
			$this->view->assign('DictionariesDataTable',$this->view->DataGrid('DictionariesDataTable',$options));
			
		}
						
	}
	
	
	/**
	 * Создание справочника
	 * 
	 * @return void
	 */
		
	public function addAction() {
		
		$form = $this->Dictionaries->getDictionaryForm('add');
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			
			if($this->Dictionaries->saveDictionaryForm($form)) {
				return $this->composeSuccessMessage('Справочник сохранен','/control/dictionaries/');
			} else {
				$this->composeTransactionErrorMessage($this->Dictionaries->_errors);
			}
			
		}

		$this->view->assign('Form',$form);
						
	}
	
	
	/**
	 * Редактирование справочника
	 * 
	 * @return void
	 */
		
	public function editAction() {
		
		if(isset($this->_params['dictionaryid']) && $DictionaryInfo = $this->Dictionaries->getDictionaryInfo($this->_params['dictionaryid'])) {
			
			$form = $this->Dictionaries->getDictionaryForm('edit');
			$form->setDefaults($DictionaryInfo);
		
			if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
				
				if($this->Dictionaries->saveDictionaryForm($form,$DictionaryInfo['dictionaryid'])) {
					return $this->composeSuccessMessage('Изменения внесены','/control/dictionaries/');
				} else {
					$this->composeTransactionErrorMessage($this->Dictionaries->_errors);
				}
				
			}
			
			$this->view->assign('Form',$form);
			
		} else {
			return $this->composeErrorMessage('Справочник с указанным Id не найден','/control/dictionaries/');
		}
						
	}
	
	
	/**
	 * Удаление справочника
	 * 
	 * @return void
	 */
		
	public function deleteAction() {
		
		if(isset($this->_params['dictionaryid']) && $DictionaryInfo = $this->Dictionaries->getDictionaryInfo($this->_params['dictionaryid'])) {
				
			if($this->Dictionaries->deleteDictionary($DictionaryInfo['dictionaryid'])) {
				return $this->composeSuccessMessage('Выбранный справочник удален','/control/dictionaries/');
			} else {
				return $this->composeDeleteErrorMessage($this->Dictionaries->_errors,'/control/dictionaries/');
			}
			
		} else {
			return $this->composeErrorMessage('Справочник с указанным Id не найден','/control/dictionaries/');
		}
						
	}
	
	
	/**
	 * Список записей в справочнике
	 * 
	 * @return void
	 */
		
	public function entriesAction() {
		
		if(isset($this->_params['dictionaryid']) && $DictionaryInfo = $this->Dictionaries->getDictionaryInfo($this->_params['dictionaryid'])) {
			
			$options = Phorm_Config::xml('grids/control/dictionaries.xml','entries');
		
			if($this->_request->isXmlHttpRequest()) {
				
				$rows = $this->Dictionaries->getEntriesList($DictionaryInfo['dictionaryid'], $this->_params);
				$paginator = $this->Dictionaries->getPaginator();
	
				$this->_helper->json(Phorm_JQuery_DataGrid::data($rows,$paginator,$options));
				
			} else {
				
				$this->view->assign('Header',$this->_translate->_('Список записей в справочнике') . ' &laquo;' . $DictionaryInfo['dictionaryname'] . '&raquo;');
				$this->view->assign('EntriesDataTable',$this->view->DataGrid('EntriesDataTable',$options));
				
			}
			
		} else {
			return $this->composeErrorMessage('Справочник с указанным Id не найден','/control/dictionaries/');
		}
						
	}
	
	
	/**
	 * Создание записи в справочнике
	 * 
	 * @return void
	 */
		
	public function addentryAction() {
		
		if(isset($this->_params['dictionaryid']) && $DictionaryInfo = $this->Dictionaries->getDictionaryInfo($this->_params['dictionaryid'])) {
		
			$this->view->assign('Header',$this->_translate->_('Создание записи в справочнике') . ' &laquo;' . $DictionaryInfo['dictionaryname'] . '&raquo;');
			
			$form = $this->Dictionaries->getEntryForm('addentry');
			
			if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
				
				$redirect = '/control/dictionaries/entries/dictionaryid/'.$DictionaryInfo['dictionaryid'].'/';
				
				if($this->Dictionaries->saveEntryForm($form,array('dictionaryid'=>$DictionaryInfo['dictionaryid']))) {
					return $this->composeSuccessMessage('Запись сохранена в справочнике',$redirect);
				} else {
					$this->composeTransactionErrorMessage($this->Dictionaries->_errors);
				}
				
			}
			
			$this->view->assign('Form',$form);
		
		} else {
			return $this->composeErrorMessage('Справочник с указанным Id не найден','/control/dictionaries/');
		}
						
	}
	
	
	/**
	 * Редактирование записи в справочнике
	 * 
	 * @return void
	 */
		
	public function editentryAction() {
		
		if(isset($this->_params['entryid']) && $EntryInfo = $this->Dictionaries->getEntryInfo($this->_params['entryid'])) {
			
			$this->view->assign('Header',$this->_translate->_('Редактирование записи в справочнике') . ' &laquo;' . $EntryInfo['dictionaryname'] . '&raquo;');
			
			$form = $this->Dictionaries->getEntryForm('editentry');
			$form->setDefaults($EntryInfo);
			
			if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
				
				$redirect = '/control/dictionaries/entries/dictionaryid/'.$EntryInfo['dictionaryid'].'/';
				
				if($this->Dictionaries->saveEntryForm($form,$EntryInfo['entryid'])) {
					return $this->composeSuccessMessage('Изменения внесены',$redirect);
				} else {
					$this->composeTransactionErrorMessage($this->Dictionaries->_errors);
				}
				
			}
			
			$this->view->assign('Form',$form);
			
		} else {
			return $this->composeErrorMessage('Запись с указанным Id не найдена','/control/dictionaries/');
		}				
	}
	
	
	/**
	 * Удаление записи в справочнике
	 * 
	 * @return void
	 */
		
	public function deleteentryAction() {
		
		if(isset($this->_params['entryid']) && $EntryInfo = $this->Dictionaries->getEntryInfo($this->_params['entryid'])) {
			
			$redirect = '/control/dictionaries/entries/dictionaryid/'.$EntryInfo['dictionaryid'].'/';
				
			if($this->Dictionaries->deleteEntry($EntryInfo['entryid'])) {
				return $this->composeSuccessMessage('Выбранная запись удалена',$redirect);
			} else {
				return $this->composeDeleteErrorMessage($this->Dictionaries->_errors,$redirect);
			}
			
		} else {
			return $this->composeErrorMessage('Запись с указанным Id не найдена','/control/dictionaries/');
		}
						
	}
	
}